package com.project.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.entity.BookInfo;
import com.project.entity.BorrowReturnInfo;
import com.project.entity.ReaderInfo;
import com.project.service.BookInfoService;
import com.project.service.BorrowReturnInfoService;
import com.project.service.ReaderInfoService;
import com.project.util.HibernateUtil;

/**
 * 
 * @author beryll@synnex.com
 * 
 */
@Controller
@RequestMapping("/borrowreturninfo")
public class BorrowReturnInfoController {

	@Resource(name = "borrowReturnInfoService")
	private BorrowReturnInfoService service;

	@Resource(name = "bookInfoService")
	private BookInfoService bookService;

	@Resource(name = "readerInfoService")
	private ReaderInfoService readerService;

	@RequestMapping(value = "/borrowconfirm")
	public String confirm(String id, String availQuantity, Model model) {
		BookInfo bookInfo = bookService.get(Integer.parseInt(id));
		model.addAttribute("bookInfo", bookInfo);
		model.addAttribute("availQuan", availQuantity);
		return "borrowreturninfo/borrowconfirm";
	}

	@RequestMapping(value = "/borrowsave")
	public String borrowreturninfo(String bookid, String readername, Model model) {
		System.out.println(bookid);
		BookInfo bookInfo = bookService.get(Integer.parseInt(bookid));
		ReaderInfo readerInfo = readerService.get(readername);
		BorrowReturnInfo borrowreturninfo = new BorrowReturnInfo();
		borrowreturninfo.setBookInfo(bookInfo);
		borrowreturninfo.setReaderInfo(readerInfo);
		borrowreturninfo.setBorrowedDate(new Date());
		borrowreturninfo.setShouldReturnDate(HibernateUtil.getDateOfNextMonth(
				new Date()).getTime());
		borrowreturninfo.setIsReturn(0);
		service.save(borrowreturninfo);
		return "redirect:/borrowreturninfo/returnquery";
	}

	@RequestMapping(value = "/returnquery")
	public String returnquery(Model model) {
		return "borrowreturninfo/returnquery";
	}

	@RequestMapping(value = "/borrowlist")
	@ResponseBody
	public Map<String, Object> list(@RequestBody Map map, Model model) {
		Map<String, Object> brmap = new HashMap<String, Object>();
		String c = map.get("chkValue").toString();
		String[] checks = c.split(",");
		List<BorrowReturnInfo> list = service.getBorrowList(map.get("bookName")
				.toString(), map.get("readerName").toString(), Integer
				.parseInt(map.get("pageSize").toString()), Integer.parseInt(map
				.get("pageNow").toString()), checks);

		int totalCount = service.borrowedCount(map.get("bookName").toString(),
				map.get("readerName").toString(), checks);

		brmap.put("list", list);
		brmap.put("totalCount", totalCount);
		return brmap;
	}

	@RequestMapping(value = "returnconfirm")
	public String returnConfirm(String id, Model model) {
		model.addAttribute("id", id);
		return "borrowreturninfo/returnconfirm";
	}

	@RequestMapping(value = "/returnsave")
	public String returnsave(String id) {
		BorrowReturnInfo borrowreturninfo = service.get(Integer.parseInt(id));
		borrowreturninfo.setReturnDate(new Date());
		borrowreturninfo.setIsReturn(1);
		int days = HibernateUtil.getIntervalDays(
				borrowreturninfo.getShouldReturnDate(), new Date());
		if (days <= 0) {
			borrowreturninfo.setOverDate(0);
		} else {
			borrowreturninfo.setOverDate(days);
		}
		service.update(borrowreturninfo);
		return "redirect:/borrowreturninfo/returnquery";
	}

	@RequestMapping(value = "/verifyReader")
	@ResponseBody
	public Map<String, Object> verifyReader(@RequestBody Map map, Model model) {
		List<BorrowReturnInfo> borrowreturnInfo = service.get(map.get(
				"queryParams").toString());// borrow return list
		Integer borrowedAmount = service.borrowedAmount(map.get("queryParams")
				.toString());// borrowed amount
		Map<String, Object> brMap = new HashMap<String, Object>();
		brMap.put("borrowreturnInfo", borrowreturnInfo);
		brMap.put("borrowedAmount", borrowedAmount);
		ReaderInfo readerInfo = readerService.getRole(map.get("queryParams")
				.toString());// reader role info
		if (readerInfo != null) {
			brMap.put("readerInfo", readerInfo);
			brMap.put("verReader", "The reader validate");
		} else {
			brMap.put("readerInfo", null);
			brMap.put("verReader", "The reader not validate,Please input again");
		}
		return brMap;
	}
}
